module diansha {
    /**
     * 数字类工厂
     * 减少对象的频繁创建，优化内存使用
     */
    export class shuzi_factory {
        private static D: shuzi_factory = null;
        private arr: Array<shuzi> = new Array<shuzi>()//数字工厂存储间(进来的都是可用的,未被用的)
        private out: shuzi;
        private min: number = 10
        private max: number = 20
        constructor() {
            for (let i = 0; i < this.min; i++) {
                this.arr.push(new shuzi(0, "n"))
            }
        }
        /**
         * 获取一个可用的对象
         */
        public get(num: number, danwei: string): shuzi {
  
            if (this.arr.length != 0) {
                this.out = this.arr.shift();
            } else {
                this.out = new shuzi(0, "n")
            }
            this.out.danwei = danwei
            this.out.num = num;

            if (this.arr.length <= this.min) {
                this.arr.push(new shuzi(0, "n"))

            }
            return this.out;
        }
        /**
         * 返还对象，回收再次利用
         */
        public give(shuzi: shuzi) {
            if (this.arr.length < this.max) {
                this.arr.push(shuzi)
            }
          

        }
        /**
         * 返回实例确保单例
         */
        public static getinstance(): shuzi_factory {
            if (this.D == null) {
                this.D = new shuzi_factory();
            }
            return this.D;
        }
    }
}